const { contextBridge, ipcRenderer } = require('electron')

window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const dependency of ['chrome', 'node', 'electron']) {
    replaceText(`${dependency}-version`, process.versions[dependency])
  }
})

contextBridge.exposeInMainWorld('api', {
  close: () => {
    ipcRenderer.send('close')
  },
  closeMsg: (cb) => {
    ipcRenderer.on('closeMsg', (event, message) => {
      cb(message)
    })
  },
  message: (cb) => {
    ipcRenderer.on('message', (event, message) => {
      cb(message)
    })
  },
  upload: async () => {
    return await ipcRenderer.invoke('upload')
  }
})
